package com.zhouchao.mvp2;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.zhouchao.mvp2.login.LoginContract;
import com.zhouchao.mvp2.login.LoginPresenter;

public class LoginActivity extends AppCompatActivity implements LoginContract.ILoginView {
    private LoginContract.ILoginPresenter loginPresenter;
    private EditText etName;
    private EditText etPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        etName=findViewById(R.id.edit_name);
        etPwd=findViewById(R.id.edit_pwd);
        loginPresenter.attachView(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginPresenter.onDestroyView();
    }

    @Override
    public String getName() {
        return etName.getText().toString().trim();
    }

    @Override
    public String getPwd() {
        return etPwd.getText().toString().trim();
    }

    @Override
    public void onClearText() {
        etName.setText("");
        etPwd.setText("");
    }

    @Override
    public void onLoginSuccess() {
        Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLogionFailure(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    private Dialog dialog;
    @Override
    public void onProgressBarShow(boolean isShow) {
        if(isShow){
            if(dialog==null){
                dialog=new Dialog(this);
                dialog.setContentView(R.layout.progress_circle);
                dialog.setCancelable(true);
            }
            if(!dialog.isShowing()){
                dialog.show();
            }
        }else{
            if(dialog!=null && dialog.isShowing()){
                dialog.dismiss();
            }
        }

    }

   /* @Override
    public LoginContract.ILoginPresenter createPresenter() {
        return loginPresenter=new LoginPresenter();
    }*/

    public void login(View view) {
        loginPresenter.login(getName(),getPwd());
    }

    public void clear(View view) {
        onClearText();
    }

    @Override
    public void createPresenter() {
        loginPresenter=new LoginPresenter();
    }
}
